/*
 * ProtocolByIdFinder.java
 *
 * Created on March 27, 2007, 11:09 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app;

import java.util.logging.Level;
import org.atomojo.app.db.DB;
import org.atomojo.app.db.Feed;
import org.infoset.xml.DocumentLoader;
import org.infoset.xml.sax.SAXDocumentLoader;
import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.resource.Finder;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class MetadataByPathFinder extends Finder {

   
   DB atomDB;
   Application theApp;
   Reference metadataBase;
   Reference resourceBase;
   DocumentLoader loader = new SAXDocumentLoader();
   Storage storage;
   App app;
   

   /** Creates a new instance of ProtocolByIdFinder */
   public MetadataByPathFinder(Context context,Application theApp,Reference metadataBase,Reference resourceBase,DB atomDB,Storage storage) {
      super(context);
      this.theApp = theApp;
      this.atomDB = atomDB;
      this.metadataBase = metadataBase;
      this.resourceBase = resourceBase;
      this.storage = storage;
      this.app = new App(context.getLogger(),atomDB,storage,theApp.getMetadataService());
      setTargetClass(ServerResource.class);
   }
   
   public ServerResource find(Request request, Response response) {
      String uriPath = request.getResourceRef().getRemainingPart();
      if (uriPath==null) {
         uriPath = request.getResourceRef().getPath();
      }
      String file = null;
      boolean isDir = uriPath.length()>0 && uriPath.charAt(uriPath.length()-1)=='/';
      String [] segments = uriPath.split("/");
      int last = segments.length-1;
      if (!isDir && segments.length>0 && segments[last].length()>2 && segments[last].indexOf('.')>0) {
         file = segments[last];
      }
      if (file==null) {
         
         // it should be a feed or entry
         
         // test to make sure it isn't an entry path
         if (!(segments.length>=2 && segments[last-1].equals("_"))) {
            try {

               Feed f = app.getFeed(uriPath);
               if (uriPath.length()>0 && !uriPath.endsWith("/")) {
                  uriPath += "/";
               }
               Reference feedResourceRef = new Reference(request.getRootRef().toString()+resourceBase.toString()+uriPath);
               Reference metadataResourceRef = new Reference(request.getRootRef().toString()+metadataBase.toString()+uriPath);
               MetadataResource r = new MetadataResource(theApp,f,request.getRootRef().toString()+resourceBase.toString(),metadataResourceRef,feedResourceRef,storage);
               return r;
            } catch (AppException ex) {
               if (ex.getStatus().getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
                  return null;
               } else {
                  getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+uriPath+" from the database.",ex);
                  return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
               }
            }
         }
      }
      return null;
   }
   
   
}
